<?php
namespace app\api\controller;

use app\common\controller\Api;
use app\common\library\Sms as Smslib;
use app\common\model\User;
use think\facade\Validate;
use think\facade\Session;
use think\Controller;
use fast\Request;
use fast\Random;
use fast\Date;
use fast\Cols;
use think\Alisms;
/**
 * 手机短信接口
 */
class Sms extends Controller
{
    protected $middleware = ['Auth',];
    protected $noNeedLogin = '*';
    protected $noNeedRight = '*';

    public function initialize(){
        parent::initialize();
    }

    /**
     * 发送验证码
     *
     * @param string $mobile 手机号
     * @param string $event 事件名称
     */
    public function send()
    {
        if($this->request->_refreshes){
            $this->error(__('请求过于频繁，请稍后再试'));
        }
        $mobile = $this->request->request("mobile");
        $event = $this->request->request("event");
        $event = $event ? $event : 'register';

        if (!$mobile || !Validate::regex($mobile, "^1\d{10}$")) {
            $this->error(__('手机号不正确'));
        }
        $last = Smslib::get($mobile, $event);
        if ($last && time() - strtotime($last['createtime']) < 20) {
            $this->error(__('发送频繁'));
        }
        if($event){
            $userinfo = model('MemberLogin')->getByMobile($mobile);
            if($userinfo){
                switch ($event) {
                    case 'register':
                        $this->error(__('手机号已被注册'));
                        break;
                    case 'changemobile':
                        $this->error(__('手机号已被占用'));
                        break;
                }
            }
        }
        /*
        $ipSendTotal = \app\common\model\Sms::where(['ip' => $this->request->ip()])->whereTime('createtime', '-1 hours')->count();
        if ($ipSendTotal >= 5) {
            $this->error(__('发送频繁'));
        }
        if ($event) {
            $userinfo = User::getByMobile($mobile);
            if ($event == 'register' && $userinfo) {
                //已被注册
                $this->error(__('已被注册'));
            } else if (in_array($event, ['changemobile']) && $userinfo) {
                //被占用
                $this->error(__('已被占用'));
            } else if (in_array($event, ['changepwd', 'resetpwd']) && !$userinfo) {
                //未注册
                $this->error(__('未注册'));
            }
        }
        */
        $r2 = Random::numeric(4);
        $aliarry = config('self.alisms');
        $alisms = new Alisms($aliarry);
        /*
        $ret = $alisms->mobile($mobile)
                ->template($aliarry['checkcode'])
                ->param(['code' => $r2])
                ->send();
        */
        $ret = Smslib::sendsms($mobile, '您的验证码是：'.$r2);
        if ($ret) {
            Session::set('checkcode', $r2);//设置session
            //发送短信日志
            $params = array();
            $params['event'] = $event;
            $params['mobile'] = $mobile;
            $params['code'] = $r2;
            $params['times'] = 1;
            $params['ip'] = $this->request->ip();
            \app\common\model\Sms::create($params);
            $this->success(__('发送成功'));
        } else {
            $this->error(__('发送失败'));
        }
    }

    /**
     * 检测验证码
     *
     * @param string $mobile 手机号
     * @param string $event 事件名称
     * @param string $captcha 验证码
     */
    public function check()
    {
        $mobile = $this->request->request("mobile");
        $event = $this->request->request("event");
        $event = $event ? $event : 'register';
        $captcha = $this->request->request("captcha");

        if (!$mobile || !\think\Validate::regex($mobile, "^1\d{10}$")) {
            $this->error(__('手机号不正确'));
        }
        if ($event) {
            $userinfo = User::getByMobile($mobile);
            if ($event == 'register' && $userinfo) {
                //已被注册
                $this->error(__('已被注册'));
            } else if (in_array($event, ['changemobile']) && $userinfo) {
                //被占用
                $this->error(__('已被占用'));
            } else if (in_array($event, ['changepwd', 'resetpwd']) && !$userinfo) {
                //未注册
                $this->error(__('未注册'));
            }
        }
        $ret = Smslib::check($mobile, $captcha, $event);
        if ($ret) {
            $this->success(__('成功'));
        } else {
            $this->error(__('验证码不正确'));
        }
    }

}
